<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/8
 * Time: 15:44
 */

namespace App\Weixin\Controllers\Admin;

use App\Base\Controllers\Controller;
use App\Weixin\Contracts\UserInterface;
use App\Weixin\Contracts\WithdrawInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class WithdrawController extends Controller
{
    /**
     * 微信用户接口
     * @return WithdrawInterface
     */
    public function getWithdrawInterface()
    {
        return app()->make(WithdrawInterface::class);
    }

    /**
     * 日志列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->getMethod() == "POST") {
            $where = function ($query) use ($request) {
                $account = $request->input("account");
                if (!empty($account)) {
                    $query->where("account", "like", '%' . $account . '%');
                }
                $account_min = $request->input("account_min", "");
                $account_max = $request->input("account_max", "");
                if (!empty($account_min) && !empty($account_max)) {
                    $query->where(["account" => array('between', array($account_min, $account_max))]);
                }
                $apply_status = $request->input("apply_status", "");
                if ($apply_status != '') {
                    $query->where("apply_status", $apply_status);
                }


                $start_date=$request->input("start_date");
                if(!empty($start_date)){
                    $query->where("create_time", ">", strtotime($start_date));
                }

                $end_date=$request->input("end_date");
                if(!empty($end_date)){
                    $query->where("create_time", "<", strtotime($end_date." 23:59:59"));
                }
            };
            return $this->getWithdrawInterface()->getList($where);
        }

        return view("weixin/withdraw/index");
    }

    /**
     * 审核通过
     * @param Request $request
     */
    public function applyTrue(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $validator = Validator::make($request->all(), [
                'id' => 'required',
            ], ["id.required" => 'ID必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $id = $request->input("id");
            $where = function ($query) use ($id) {
                $query->where("id", $id);
            };
            $data["apply_status"] = '1';
            //审请通过后，就更新用户权限级
            return $this->getWithdrawInterface()->updateData($where, $data);
        }
    }

    public function applyError(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $validator = Validator::make($request->all(), [
                'id' => 'required',
                'msg' => 'required',
            ], ["id.required" => 'ID必须有值', "msg.required" => '拒绝理由必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $id = $request->input("id");
            $where = function ($query) use ($id) {
                $query->where("id", $id);
            };
            $msg = $request->input("msg");
            $data["apply_status"] = '-1';
            $data["apply_err_msg"] = $msg;
            return $this->getWithdrawInterface()->balckUserData($where, $data);
        }
    }

    /**
     * 显示
     * @param Request $request
     */
    public function show(Request $request)
    {
        $where = function ($query) use ($request) {
            $id = $request->input("id");
            $query->where("id", $id);
        };
        $info=$this->getWithdrawInterface()->getInfo($where);
        //获取手续费
       $poundage= $this->getWithdrawInterface()->getPoundage($info->amount);
        return view("weixin/withdraw/show")->with("info",$info)->with("poundage",$poundage);
    }
}